/**
  * 牌局记录
  */
module game {

    export class JiLuMediator extends BaseMediator {
        public static NAME: string = "JiLuMediator";
        private obj;
        public constructor(viewComponent: any = null) {
            super(JiLuMediator.NAME, viewComponent);
        }

        public listNotificationInterests(): Array<any> {
            return [
                PanelNotify.HALL.STAGE_RESIZE,
                PanelNotify.HALL.OPEN_JILU,
                PanelNotify.HALL.CLOSE_JILU
            ];
        }
        private panel: JiLuPanel;
        public handleNotification(notification: puremvc.INotification): void {

            switch (notification.getName()) {
                case PanelNotify.HALL.STAGE_RESIZE:
                    if (this.panel) {
                        this.panel.width = GameConfig.curWidth()
                        this.panel.height = GameConfig.curHeight()
                    }
                    break;
                case PanelNotify.HALL.OPEN_JILU: {
                    this.obj = notification.getBody();
                    this.panel = null;
                    this.panel = new JiLuPanel();
                    this.showUI(this.panel, 2, 1, true);
                    break;
                }
                case PanelNotify.HALL.CLOSE_JILU: {
                    this.closePanel(0);
                    break;
                }
            }
        }

        /**
         * 初始化面板ui
         */
        public initUI(): void {
            this.panel.closeBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.closeButtonClick, this);
        }

        /**
         * 初始化面板数据
         */
        public initData(): void {
            //根据传过来的游戏类型和房间等级查询对应的牌局记录 this.gameType
            var obj = JSON.parse(this.obj)
            var gameType = obj["gameType"]
            var roomType = obj["roomType"]

            var wKindID
            if (gameType == InstructionsEnum.GAME_TYPE_NAME.DDZ) {
                wKindID = InstructionsEnum.GAME_SERVER_ID.DDZ
            }
            if (gameType == InstructionsEnum.GAME_TYPE_NAME.QZNN) {
                wKindID = InstructionsEnum.GAME_SERVER_ID.QZNN
            }
            if (gameType == InstructionsEnum.GAME_TYPE_NAME.BRNN) {
                wKindID = InstructionsEnum.GAME_SERVER_ID.BRNN
            }
            if (gameType == InstructionsEnum.GAME_TYPE_NAME.HLSZ) {
                wKindID = InstructionsEnum.GAME_SERVER_ID.HLSZ
            }
            if (gameType == InstructionsEnum.GAME_TYPE_NAME.WJL) {
                wKindID = InstructionsEnum.GAME_SERVER_ID.WJL
            }
            if (gameType == InstructionsEnum.GAME_TYPE_NAME.LHD) {
                wKindID = InstructionsEnum.GAME_SERVER_ID.LHD
            }
            if (gameType == InstructionsEnum.GAME_TYPE_NAME.HHDZ) {
                wKindID = InstructionsEnum.GAME_SERVER_ID.HHDZ
            }
            if (gameType == InstructionsEnum.GAME_TYPE_NAME.BREBG) {
                wKindID = InstructionsEnum.GAME_SERVER_ID.BREBG
            }
            var request = new egret.HttpRequest();
            request.responseType = egret.HttpResponseType.TEXT;
            request.open(InstructionsEnum.IP_API + "/gameinfo/getdrawscore?uid=" + GameVO.getVo().dwUserID + "&kindid=" + wKindID + "&roomType=" + roomType, egret.HttpMethod.GET);
            request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            request.send();
            request.addEventListener(egret.Event.COMPLETE, (event: egret.Event) => {
                try {
                    var request = <egret.HttpRequest>event.currentTarget;
                    var obj = JSON.parse(request.response)
                    var map = obj["map"];
                    this.showData(map)
                } catch (err) {
                    EffectUtils.textFadeAway("数据包异常，无法解析!")
                }
            }, this);
        }
        private showData(map: Object) {
            this.panel.data_group.removeChildren();
            var num = 1;
            var y = 0
            if (Object.keys(map).length == 0) {
                this.panel.wushuju.visible = true;
            }
            for (var i in map) {
                var group = new eui.Group();
                group.y = y
                var SerialNumber = map[i]["SerialNumber"]//牌局编号
                var ServerName = map[i]["ServerName"]//房间名字
                var TotalScore = map[i]["TotalScore"]//有效投资
                var Score = map[i]["Score"]//盈利
                var InsertTime = map[i]["InsertTime"]//结束时间
                if (num % 2 == 0) {
                    var bg = new eui.Image("jilu_link_0")
                    group.addChild(bg)
                }
                var xuhao = new eui.Label(num + "")
                xuhao.width = 60;
                xuhao.textAlign = "center";
                xuhao.x = 17
                xuhao.y = 20;
                xuhao.size = 18
                xuhao.fontFamily = "Microsoft YaHei"
                xuhao.textColor = 0xb9a49c
                group.addChild(xuhao)

                var juhao = new eui.Label(SerialNumber + "")
                juhao.x = 100;
                juhao.y = 20
                juhao.size = 18
                juhao.fontFamily = "Microsoft YaHei"
                juhao.textColor = 0xb9a49c
                group.addChild(juhao)

                var name = new eui.Label(ServerName + "")
                name.x = 435
                name.y = 20
                name.size = 18;
                name.width = 150;
                name.textAlign = "center";
                name.fontFamily = "Microsoft YaHei"
                name.textColor = 0xb9a49c
                group.addChild(name)

                var xiazhu = new eui.Label(TotalScore + "")
                xiazhu.x = 630
                xiazhu.y = 20
                xiazhu.size = 18;
                xiazhu.width = 120;
                xiazhu.textAlign = "center";
                xiazhu.fontFamily = "Microsoft YaHei"
                xiazhu.textColor = 0xb9a49c
                group.addChild(xiazhu)

                var yingli = new eui.Label(Score + "")
                yingli.x = 810
                yingli.y = 20
                yingli.size = 18
                yingli.width = 120;
                yingli.textAlign = "center";
                yingli.fontFamily = "Microsoft YaHei"
                yingli.textColor = Number(Score) > 0 ? 0x8aff00 : 0xff0000
                group.addChild(yingli)

                var shijian = new eui.Label(InsertTime + "")
                shijian.x = 970
                shijian.y = 18
                shijian.size = 20
                shijian.fontFamily = "Microsoft YaHei"
                shijian.textColor = 0xb9a49c
                group.addChild(shijian)
                this.panel.data_group.addChild(group)
                y += 60;
                num++;
            }

        }
        private closeButtonClick(event: egret.TouchEvent): void {
            SoundManage.startAnniuBtnLoad();
            this.closePanel(1);
        }
    }
}